package com.songlanyun.modules.order.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.OrderConstant;
import com.songlanyun.common.utils.SpringContextUtils;
import com.songlanyun.common.utils.custom.DateCustomUtils;
import com.songlanyun.modules.order.service.ChildOrderServiceItemService;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 服务订单核销卡
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2021-03-02 16:01:16
 */
@Data
@TableName("yxt_sale_service_goods_card")
public class SaleServiceGoodsCard extends RootEntity<Long, SaleServiceGoodsCard> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 主订单id
     */
    @NotNull(message = "主订单id不能为空")
    private Long orderId;
    /**
     * 子订单id
     */
    @NotNull(message = "子订单id不能为空")
    private Long childOrderId;
    /**
     * 服务商品有效天数
     */
    private Integer validity;
    /**
     * 服务商品提前预约天数
     */
    private Integer advanceReservation;
    /**
     * 服务商品免费福利项目
     */
    private String welfareItems;
    /**
     * 服务商品服务次数
     */
    private Integer serviceCount;
    /**
     * 核销码（每核销成功一次刷新一次）
     */
    private String verificationCode;
    /**
     * 最后一次的核销时间
     */
    private Date lastCheckTime;
    /**
     * 第一次核销时间
     */
    private Date firstCheckTime;
    /**
     * 已核销的次数
     */
    private Integer useCount;

    public void incUseCount()
    {
        this.useCount = this.useCount+1;
    }

    /**
     * 剩余使用次数
     * @return
     */
    public Integer getResidueUseCount()
    {
        return this.serviceCount - this.useCount;
    }

    /**
     * 服务订单的使用状态
     * @return
     */
    public OrderConstant.ServiceGoodsUseStatus getServiceGoodsUseStatus()
    {
        if(this.getExpiredUseTime() ==0){

            return OrderConstant.ServiceGoodsUseStatus.NOT_USE;
        }

        if(this.getExpiredUseTime() < System.currentTimeMillis())
        {
            return OrderConstant.ServiceGoodsUseStatus.EXPIRED;
        }

        if(this.getResidueUseCount()<=0)
        {
            return OrderConstant.ServiceGoodsUseStatus.COMPLETE;
        }

        return OrderConstant.ServiceGoodsUseStatus.ABLE_USE;
    }

    @NotNull(message = "使用过期时间不能为空")
    private Long expiredUseTime;

    public void setExpiredUseTime(Long expiredUseTime) {
        this.expiredUseTime = expiredUseTime;
        if (expiredUseTime > 0) {
            this.formatExpiredUseTime = DateCustomUtils.formatToDateTimeString(expiredUseTime);
        }
    }

    /**
     * 格式化使用过期时间（时间戳）
     */
    @TableField(exist = false)
    private String formatExpiredUseTime;


    @TableField(exist = false)
    private List<ChildOrderServiceItem> serviceItemList;

    public List<ChildOrderServiceItem> loadServiceItems() {

        if (serviceItemList == null) {
            ChildOrderServiceItemService service = ((ChildOrderServiceItemService) SpringContextUtils.getBean("childOrderServiceItemService"));
            serviceItemList = service.loadServiceItem(getChildOrderId());
        }

        return serviceItemList;
    }

    @Override
    public Long getIdentity() {
        return getId();
    }
}
